using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace OnlineAnadolu
{
    public partial class UrunDetay5 : System.Web.UI.Page
    {
        Database db = new Database();
        string d = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string s = "Select deadline from products where productid='" + Session["idproduct"].ToString() + "'";
                DataTable dt = new DataTable();
                dt = db.select(s);
                foreach (DataRow dr in dt.Rows)
                {
                    s = dr["deadline"].ToString();
                }
                cal_SonTarih.SelectedDate = Convert.ToDateTime(s);
                d = (((Image)DetailsView1.FindControl("Image1")).ImageUrl);
                d = d.Substring(7);
            }
        }

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            //string a = (DetailsView1.Rows[0]..Text).ToString();
            //string s = "UPDATE products SET name = N'" + DetailsView1.Rows[1].Cells[1].Text + "', description =  N'" + DetailsView1.Rows[2].Cells[1].Text + "', price = N'" + DetailsView1.Rows[3].Cells[1].Text + "', productdate = N'" + DetailsView1.Rows[4].Cells[1].Text + "' , deadline = '" + Convert.ToDateTime(DetailsView1.Rows[5].Cells[1].Text).ToString("MM.dd.yyyy") + "', available = 5 WHERE (products.productid = '" + Session["idproduct"].ToString() + "')";
            //SqlDataSource1.UpdateCommand = "UPDATE products SET name = N'" + DetailsView1.Rows[1].Cells[1].Text + "', description =  N'" + DetailsView1.Rows[2].Cells[1].Text + "', price = N'" + DetailsView1.Rows[3].Cells[1].Text + "', productdate = N'" + DetailsView1.Rows[4].Cells[1].Text + "' , deadline = '" + Convert.ToDateTime(DetailsView1.Rows[5].Cells[1].Text).ToString("MM.dd.yyyy") + "', available = 5 WHERE (products.productid = '" + Session["idproduct"].ToString() + "')";
            //SqlDataSource1.Update();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            lb_Resim.Text = "";
            if (myFileUpload.HasFile)
            {
                if ((myFileUpload.PostedFile.ContentLength < 1000000) && (myFileUpload.PostedFile.ContentType == "image/pjpeg") || (myFileUpload.PostedFile.ContentType == "image/gif") ||
(myFileUpload.PostedFile.ContentType == "image/jpg") ||
(myFileUpload.PostedFile.ContentType == "image/jpeg") ||

                (myFileUpload.PostedFile.ContentType == "image/bmp"))
                {
                    lb_Resim.Text = ((-1) * DateTime.Now.ToBinary()) + myFileUpload.FileName;


                    myFileUpload.SaveAs(Server.MapPath("image/" + lb_Resim.Text));

                    lb_Resim2.Text = myFileUpload.FileName + " adli resminiz yuklendi";
                }
                else
                    lb_Resim2.Text = "Resiminiz boyutu cok buyuk";
            }
            else
            {
                lb_Resim2.Text = "*";
            }

            myFileUpload.Focus();
        }

        protected void bt_Guncelle_Click(object sender, EventArgs e)
        {
            string c="";
           
            if (lb_Resim.Text == "")
                c = ((Image)DetailsView1.FindControl("Image1")).ImageUrl;
            else
            {
                File.Delete("//image//"+d);                
                c = "image\\" + lb_Resim.Text;

            }

            string f = "UPDATE products SET deadline='" + cal_SonTarih.SelectedDate.ToString("MM.dd.yyyy") + "' where productid='"+Session["idproduct"].ToString()+"'";
            db.insert(f);
            f = "Update productimage set productimage=N'" + c + "' where productimageid=(Select imageid from products where productid='" + Session["idproduct"].ToString() + "')";
            db.insert(f);
            lb_Guncelle.Text = "Urununuz guncellendi";
            DetailsView1.DataBind();
        }

        protected void bt_Tamam2_Click(object sender, EventArgs e)
        {
            if (cal_SonTarih.SelectedDate <= DateTime.Now)
                lb_TarihUyari.Text = "Bugunden daha buyuk bir tarih seciniz";
            else
                lb_TarihUyari.Text = cal_SonTarih.SelectedDate.ToString();
        }

        protected void bt_Iptal_Click(object sender, EventArgs e)
        {
            Response.Redirect("bak.aspx");
        }
    }
}
